import {Injectable} from '@angular/core';
import {Http, Headers, Response, RequestOptions} from '@angular/http'

import 'rxjs/add/operator/toPromise';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {Router} from "@angular/router";
import {CommonIp} from "../../user/entity/Ip";


@Injectable()
export class ProjectDeleteService extends CommonIp{
  private DelectProjectStepURL=this.userIp+"/p/projectStep/delete/"
  private deleteImgURL = this.userIp+"/p/project/deleteShowPic/";
  private deleteProjectURL = this.userIp+"/p/project/projectDelete/";
 constructor(public http:Http,public router:Router){
    super();
  }

  public DeleteProgectStep(id:string):Promise<any>{
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});

    return this.http.get(this.DelectProjectStepURL+id,options)
      .toPromise()
      .then(this.extractDeleteData).catch(this.handleError);
  }
  private extractDeleteData(res:Response){

    if(res.status<200||res.status>=300) {
      throw  new Error('Bad response status:' + res.status);
    }
    return res.json();
  }

  private extractData(res: Response) {
    let result = res.json();
    return result || { };
  }
  private handleError (error: Response | any) {
    // In a real world app, you might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }


  deleteShowPicById(id: string, p: string):Promise<any> {

    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});

    return this.http.post(this.deleteImgURL+id+"?url="+p,options)
      .toPromise()
      .then(this.extractDeleteData).catch(this.handleError);
  }

  public deleteProject(id: string) {
    let headers = new Headers({'Content-Type':'application/json'});
    let options = new RequestOptions({headers:headers});

    return this.http.post(this.deleteProjectURL+id,options)
      .toPromise()
      .then(this.extractDeleteData).catch(this.handleError)
  }
}
